package com.xrit.sunnyday.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xrit.sunnyday.dao.CheckGroupDao;
import com.xrit.sunnyday.entity.QueryPageBean;
import com.xrit.sunnyday.exception.SunnydayException;
import com.xrit.sunnyday.pojo.CheckGroup;
import com.xrit.sunnyday.pojo.CheckItem;
import com.xrit.sunnyday.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 类名称: CheckGroupServiceImpl
 * 类描述:TODO
 * 创建人: czb
 * 创建时间:2022/6/11 上午 11:25
 * Version 1.0.0
 */
@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImpl implements CheckGroupService {
    @Autowired
  private CheckGroupDao checkGroupDao;

    /*分页查询*/
    @Override
    public PageInfo<CheckGroup> findPage(QueryPageBean queryPageBean) {
//        开启分页
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        if (!StringUtils.isEmpty(queryPageBean.getQueryString())){
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }
     List<CheckGroup> list= checkGroupDao.findPage(queryPageBean.getQueryString());
        return new PageInfo<>(list);
    }

    @Override
    public CheckGroup findById(Integer id) {
        return checkGroupDao.findById(id);
    }

    @Override
    public void update(CheckGroup checkGroup,Integer[] checkitemIds) {
        checkGroupDao.update(checkGroup);
        //先进行删除再添加
        checkGroupDao.deleteGroupItem(checkGroup.getId());
        if (checkitemIds.length>0){
            HashMap<String, Object> map = new HashMap<>();
            map.put("groupId",checkGroup.getId());
            map.put("ItemIds",checkitemIds);
            checkGroupDao.updateGroupItem(map);
        }

    }

    @Override
    public void addCheckGroupAndItem(CheckGroup checkGroup, Integer[] checkitemIds) {
        //根据长度判断数组里面有没有数据
        checkGroupDao.add(checkGroup);
        System.out.println(checkGroup.getId());
        if (checkitemIds.length>0){
            HashMap<String, Object> map = new HashMap<>();
            map.put("checkGroupId",checkGroup.getId());
            map.put("checkItemIds",checkitemIds);
            checkGroupDao.addCheckGroupAndItem(map);
        }
    }

    @Override
    public List<Integer> findByCheckItemId(Integer id) {
//        进行转换
        ArrayList<Integer> list = new ArrayList<>();
        List<CheckItem> byCheckItemId = checkGroupDao.findByCheckItemId(id);
        for (CheckItem checkItem : byCheckItemId) {
            list.add(checkItem.getId());
        }
        return list;
    }

    @Override
    public void deleteById(Integer id)throws SunnydayException {
//        先查询有没有绑定套餐
      int i =  checkGroupDao.findCountGroup(id);
      if (i>0){
          throw new SunnydayException("您的数据已经绑定了套餐");
      }
//      再删除表先删除与检查项的中间表
        checkGroupDao.deleteGroupItem(id);
      checkGroupDao.deleteGroupItemId(id);
    }

    @Override
    public List<CheckGroup> findAll() {
        return checkGroupDao.findAll();
    }
}
